home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / lists / mint / l_0799 / 516 < prev    next >
Encoding:
Internet Message Format  |  1994-08-27  |  3.9 KB

  1. Subject: put shells etc. on (virtual) tty without init...
  2. Date: Wed, 29 Sep 93 23:42:26 CET
  3. From: Juergen Lock <nox@jelal.north.de>
  4. Message-Id: <9309292242.AA00164@jelal.north.de>
  5.  
  6. here's a little hack that puts a command or shell on a terminal.  useful
  7. when you don't have enough RAM for init _and_ gcc...  (or when you don't
  8. like init for some other reason though i can't think of one :-)
  9.  
  10.  easiest way, and also a good place in terms of memory fragmentation is
  11. to put it in a mint.cnf:  (otherwise it can be called from anywhere.)
  12.  
  13. # first start virtual consoles... (of course other ttys should also work :)
  14. exec u:\c\usr\etc\vcon.ttp
  15. ren u:\dev\console u:\dev\con0
  16. ren u:\dev\vt00 u:\dev\console
  17. CON=u:\dev\console
  18. # turn on cursor (make the ^[ one esc char)
  19. echo ^[e
  20. # set $SHELL (default is /bin/sh)
  21. setenv SHELL u:\bin\ksh.ttp
  22. # (and also $HOME, $TERM...)
  23. cd u:\home\nox
  24. # init for console (could also be GEM... i prefer to start it from the
  25. # shell when i need it)
  26. INIT=c:\bin\ksh.ttp -L
  27.  
  28. # everything set, now put a top on vt01...
  29. exec u:\c\local\bin\runtt.ttp -t vt01 top
  30. # and 2 (more) shells on vt02 and 3
  31. exec u:\c\local\bin\runtt.ttp -t vt02
  32. exec u:\c\local\bin\runtt.ttp -t vt03
  33. # maybe add a sleep here so the shells have time to come up before GEM.
  34.  
  35.  again, comments welcome...
  36.     Juergen
  37.  
  38. -------cut-here------------
  39. /* runtt -- poor man's init :) for MiNT
  40.    open tty, setpgrp(), exec command on it...
  41.  
  42.    usage: runtt command >tty
  43.       or: runtt -T tty command  (empty command gets loginshell)
  44.       or: runtt -t tty command  (like -T but (t)forks before.)
  45. */
  46.  
  47. #include <stdio.h>
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <signal.h>
  51. #include <setjmp.h>
  52. #include <ioctl.h>
  53. #include <fcntl.h>
  54. #include <unistd.h>
  55. #include <support.h>
  56. #include <mintbind.h>
  57. #include <errno.h>
  58.  
  59. static jmp_buf    tforkj;
  60.  
  61. static int in_tfork(arg)
  62. int arg;
  63. {
  64.     /* wait for parent to die before we can longjmp back */
  65.     while (getppid () > 1)
  66.         (void) Fselect (1000, 0l, 0l, 0l);
  67.     longjmp (tforkj, 1);
  68.     /*NOTREACHED*/
  69. }
  70.  
  71. static jmp_buf stopj;
  72.  
  73. void ttout(sig)
  74. int sig;
  75. {
  76.     longjmp (stopj, 1);
  77. }
  78.  
  79. int main(argc, argv)
  80. int argc;
  81. char **argv;
  82. {
  83.     long pgrp = setpgrp(/*getpid(), getpid()*/);
  84.     static char *name, buf[0x100] = "/dev/", **xargv;
  85.     static int tty, do_fork = 0;
  86.     char *shell;
  87.  
  88.     xargv = argv;
  89.     if (argv[1] && argv[1][0] == '-' && (argv[1][1] | 0x20) == 't'
  90.         && !argv[1][2] && (name = argv[2])) {
  91.         if (*name != '/' && *name != '\\' &&
  92.             (!*name || name[1] != ':')) {
  93.             strcat (buf, name);
  94.             name = buf;
  95.         }
  96.         if (argv[1][1] == 't')
  97.             do_fork = 1;
  98.         tty = open(name, O_RDWR);
  99.         xargv += 2;
  100.     } else {
  101.         name = ttyname(1);
  102.         tty = dup(1);
  103.     }
  104.  
  105.     if (tty == -1 || pgrp < 0) {
  106.         signal (SIGTTOU, SIG_IGN);
  107.         perror(name);
  108.         exit(1);
  109.     }
  110.     /* tty in use?  stat doesn't tell on MiNT, so... */
  111.     signal (SIGTTOU, ttout);
  112.     if (_isctty(tty) || setjmp (stopj)) {
  113.         signal (SIGTTOU, SIG_IGN);
  114.         fprintf(stderr, "%s: tty in use\n", name);
  115.         exit(1);
  116.         /* keep compiler happy */
  117.         return 1;
  118.     }
  119.     /* write NUL to trigger SIGTTOU... */
  120.     Fputchar (tty, 0, 0);
  121.     /* if we get here there was no sig */
  122.     if (do_fork) {
  123.         Fcntl (tty, 0, F_SETFD);
  124.         if (!setjmp(tforkj) && tfork (in_tfork, 0) >= 0)
  125.             _exit (0);
  126.     }
  127.     dup2(tty, -1);
  128.     ioctl (-1, TIOCSPGRP, &pgrp);
  129.  
  130.     dup2(tty, 0);
  131.     dup2(tty, 1);
  132.     dup2(tty, 2);
  133.  
  134.     close(tty);
  135.  
  136.     shell = getenv("SHELL");
  137.     if (!shell)
  138.         shell = "/bin/sh";
  139.     if (!xargv[1] ||
  140.         (execvp((name = xargv[1]), xargv+1) < 0 &&
  141.          (errno == ENOEXEC || errno == ENOENT))) {
  142.         name = shell;
  143.  
  144.         /* loginshell? */
  145.         if (!xargv[1]) {
  146.             char *p, *q;
  147.  
  148.             for (p = shell; (q = strpbrk(p, "/\\:"));)
  149.                 p = ++q;
  150.             xargv[0] = q = buf;
  151.             *q++ = '-';
  152.             strcpy (q, p);
  153.         } else
  154.             xargv[0] = shell;
  155.         execv(shell, xargv);
  156.     }
  157.     perror(name);
  158.     exit(1);
  159.     return 1;
  160. }
  161. -- 
  162. J"urgen Lock / nox@jelal.north.de / UUCP: ..!uunet!unido!uniol!jelal!nox
  163.                                 ...ohne Gewehr
  164. PGP public key fingerprint =  8A 18 58 54 03 7B FC 12  1F 8B 63 C7 19 27 CF DA 
  165.